<?php
class ManageModel extends Model {
    public function __construct() {
        parent::__construct();
        //manage表需要的字段
        $this->fields = array('id','cPass','user','pass','level','login_count','last_ip','last_time','reg_time');
        $this->tables = array(DB_FREFIX.'manage');
        $this->check = new ManageCheck();
        //转义post和get获得的各种参数
        list(
                $this->R['id'],
                $this->R['user'],
                $this->R['pass'],
                $this->R['code']
        ) = $this->getRequest()->getParams(
            array(
                //加上一层判断消除notice警告
                isset($_GET['id']) ? $_GET['id'] : null,
                isset($_POST['user']) ? $_POST['user'] : null,
                isset($_POST['pass']) ? sha1($_POST['pass']) : null,
                isset($_POST['code']) ? $_POST['code'] : null
            )
        );
    }

    //查询多条
    public function findAll() {
        $this->tables = array(DB_FREFIX.'manage m',DB_FREFIX.'level l');
        return parent::select($this->tables,array('m.id','m.user','m.level','m.login_count','m.last_ip','m.last_time','l.level_name'),
                              array('where'=>array('m.level=l.id'),'limit'=>$this->limit,'order'=>'m.reg_time DESC'));
    }

    //查询单条
    public function findOne() {
        $condition = array('id='.$this->R['id']);
        if(!$this->check->oneCheck($this,$condition)) $this->check->displayError();   //因为参数是从网址得来的，因此需要经过处理和过滤
        return parent::select($this->tables,array('id','pass','user','level'),array('where'=>$condition,'limit'=>'1'));
    }

    /**
     * 新增管理员,此处使用委托的方法来调用add方法，通过调用父类model的add方法，而model类又
     * 调用了它的父类，也就是该model的祖父类的方法，这样该类就负责数据进行数据过滤，而model
     * 类则负责使该类与db进行通信，而db类的职责就是调用sql语句进行数据库操作，体现了各个类的
     * 单一职责原则
     */
    public function add() {
        $condition = array('user='.$this->R['user']);
        //验证发送的数据
        if(!$this->check->addCheck($this,$condition)) $this->check->displayError();
        //过滤不必要的数据
        $needData = $this->getRequest()->filter($this->fields);
        //默认和特殊字段的系统自动填充和修正
        $needData['pass'] = sha1($needData['pass']);
        $needData['last_ip'] = Tool::getIp();
        $needData['reg_time'] = Tool::getDate();
        return parent::add($needData);
    }

    public function update() {
        $condition = array('id='.$this->R['id']);
        //验证数据是否存在
        if(!$this->check->oneCheck($this,$condition)) $this->check->displayError();
        //验证数据是否合法
        if(!$this->check->updateCheck($this)) $this->check->displayError();
        //接收过滤和格式化后的数据
        $data = $this->getRequest()->filter($this->fields);
        $data['pass'] = sha1($data['pass']);
        //原密码如果没有被修改，也就是修改表单中的密码留空则为不修改，则把原密码作为pass的字段值
        if(isset($data['pass']) && $data['pass'] == sha1('')) {
            $data['pass'] = $data['cPass'];
        }
        unset($data['cPass']);
        return parent::update($data,$condition);
    }

    //删除
    public function delete() {
        $condition = array('id='.$this->R['id'],'limit'=>1);
        if(!$this->check->oneCheck($this,$condition)) $this->check->displayError();
        return parent::delete($condition);
    }

    //查找管理员和对应的等级
    public function findLogin() {
        $this->tables = array(DB_FREFIX.'manage m',DB_FREFIX.'level l');
        return parent::select($this->tables,array('m.user','l.level_name'),array('where'=>array('m.level=l.id','rule'=>'AND','m.user='.$this->R['user'])));
    }

    //ajax验证用户名唯一性
    public function checkUserName() {
        $condition = array('user='.$this->R['user']);
        $this->check->checkUserName($this,$condition);
    }

    //管理员总记录条数
    public function total() {
        return parent::total();
    }

    //检查管理员登陆
    public function login() {
        $condition = array('user='.$this->R['user'],'pass='.$this->R['pass']);
        if(!$this->check->loginCheck($this,$condition)) {
            $this->check->displayError();
        } else {
            return true;
        }
    }

    //改变登录信息
    public function changeLoginInfo() {
        $updateData['login_count'] = array('login_count+1');
        $updateData['last_ip'] = Tool::getIp();
        $updateData['last_time'] = Tool::getDate();
        $condition = array('user='.$this->R['user']);
        return parent::update($updateData,$condition);
    }

    //ajax验证验证码正确与否
    public function ajaxCheckCode() {
        $this->check->ajaxCheckCode($this);
    }

}


?>